package com.xinyin.aiqinhaiback.common;

import com.xinyin.aiqinhaiback.modbus.Modbus4jUtils;
import com.xinyin.aiqinhaiback.model.entity.Information;
import com.xinyin.aiqinhaiback.model.entity.SensorInfo;
import com.xinyin.aiqinhaiback.service.impl.InformationServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Component
public class ConfigCache {
	// 用于存储传感器和设备数据的并发映射
	public static ConcurrentMap<String, List> data = new ConcurrentHashMap<>();
	// 用于存储设备IP地址和端口的并发映射
	public static ConcurrentMap<String, String> address = new ConcurrentHashMap<>();

	// 常量IP地址
	private static final String COLD_IP = "192.168.1.10"; // 冷源IP
	private static final String HEAT_IP = "192.168.1.20"; // 热源IP
	private static final String COOLING_TOWER_IP = "192.168.1.9"; // 冷却塔IP

	// 风机IP地址映射
	private static final Map<String, String> FAN_IPS;
	static {
		Map<String, String> fanIpsMap = new HashMap<>();
		fanIpsMap.put("fan1_1Ip", "192.168.1.31");
		fanIpsMap.put("fan1_2Ip", "192.168.1.32");
		fanIpsMap.put("fan1_3Ip", "192.168.1.33");
		fanIpsMap.put("fan1_4Ip", "192.168.1.34");
		fanIpsMap.put("fan1_5Ip", "192.168.1.35");
		fanIpsMap.put("fan2_1Ip", "192.168.1.41");
		fanIpsMap.put("fan2_2Ip", "192.168.1.42");
		fanIpsMap.put("fan2_3Ip", "192.168.1.43");
		fanIpsMap.put("fan2_4Ip", "192.168.1.44");
		fanIpsMap.put("fan3_1Ip", "192.168.1.51");
		fanIpsMap.put("fan3_2Ip", "192.168.1.52");
		fanIpsMap.put("fan3_3Ip", "192.168.1.53");
		fanIpsMap.put("fan3_4Ip", "192.168.1.54");
		fanIpsMap.put("fan4_1Ip", "192.168.1.61");
		fanIpsMap.put("fan4_2Ip", "192.168.1.62");
		fanIpsMap.put("fan4_3Ip", "192.168.1.63");
		FAN_IPS = Collections.unmodifiableMap(fanIpsMap); // 使其不可变
	}

	private static final String PORT = "502"; // 端口号

	// 用于存储Modbus工具的并发映射
	public static ConcurrentHashMap<String, Modbus4jUtils> modbusList = new ConcurrentHashMap<>();

	@Autowired
	private InformationServiceImpl informationServiceImpl;

	@PostConstruct
	public void init() {
		// 初始化传感器和设备数据
		List<SensorInfo> sensors = informationServiceImpl.getAllSensors();
		List<Information> devices = informationServiceImpl.getAlldevices();
		data.put("sensors", sensors);
		data.put("devices", devices);

		// 填充地址映射
		address.put("heatIp", HEAT_IP);
		address.put("coldIp", COLD_IP);
		address.put("coolingTowerIp", COOLING_TOWER_IP);
		address.putAll(FAN_IPS); // 将风机IP地址添加到地址映射中
		address.put("port", PORT);

		// 初始化Modbus工具
		initializeModbusUtils();
	}

	// 初始化Modbus工具的方法
	private void initializeModbusUtils() {
		modbusList.put("heatModbus", new Modbus4jUtils(HEAT_IP, PORT));
		modbusList.put("coldModbus", new Modbus4jUtils(COLD_IP, PORT));
		modbusList.put("coolingTowerModbus", new Modbus4jUtils(COOLING_TOWER_IP, PORT));

		// 初始化风机Modbus工具
		FAN_IPS.forEach((key, ip) ->
				modbusList.put(key.replace("Ip", "Modbus"), new Modbus4jUtils(ip, PORT))
		);
	}
}
